package com.atguigu.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.atguigu.common.result.R;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.entity.LendItem;
import com.atguigu.srb.core.pojo.vo.InvestVo;
import com.atguigu.srb.core.service.LendItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Bright
 * @since 2022-04-09
 */
@Slf4j
@Api(tags = "投资记录")
@RestController
@RequestMapping("/api/core/lendItem")
public class LendItemController {
    @Resource
    LendItemService lendItemService;

    @ApiOperation("投标")
    @PostMapping("/auth/commitInvest")
    public R commitInvest(@RequestBody InvestVo investVo, HttpServletRequest request){
        String token = request.getHeader("token");
        investVo.setInvestUserId(JwtUtils.getUserId(token));

        return R.ok().data("formStr",lendItemService.commitInvest(investVo));

    }

    @ApiOperation("投标回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        Object resultCode = paramMap.get("resultCode");
        if (HfbConst.SUCCESSCODE.equals(resultCode)){
            if (RequestHelper.isSignEquals(paramMap)){

                return lendItemService.notify(paramMap);
            }else {
                log.warn("用户投资异步回调签名错误：" + JSON.toJSONString(paramMap));
                return "fail";
            }
        }else {
            log.warn("用户投资异步回调失败：" + JSON.toJSONString(paramMap));
            return "fail";
        }

    }

    @ApiOperation("获取列表")
    @GetMapping("/list/{lendId}")
    public R list(
            @ApiParam(value = "标的id", required = true)
            @PathVariable Long lendId) {
        List<LendItem> list = lendItemService.listByLendId(lendId);
        return R.ok().data("list", list);
    }






}

